/*
 * Copyright (C) 2011-2013 Eric Lasota
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#include "rdx_basictypes.hpp"
#include "rdx_longflow.hpp"
#include "rdx_io.hpp"

namespace RDX
{
	namespace Programmability
	{
		void DecodeString(const Char *s, Int64 &i);
		void DecodeString(const Char *s, Int32 &i);
		void DecodeString(const Char *s, Int16 &i);
		void DecodeString(const Char *s, Int8 &i);
		void DecodeString(const Char *s, UInt64 &i);
		void DecodeString(const Char *s, UInt32 &i);
		void DecodeString(const Char *s, UInt16 &i);
		void DecodeString(const Char *s, UInt8 &i);
		void DecodeString(const Char *s, Float32 &f);
		void DecodeString(const Char *s, Float64 &f);
#ifdef RDX_WCHAR_T_IS_NATIVE
		void DecodeString(const Char *s, wchar_t &f);
#endif
		int EncodeString(Char *s, Int64 i);
		int EncodeString(Char *s, Int32 i);
		int EncodeString(Char *s, UInt8 i);
		int EncodeString(Char *s, UInt16 i);
		int EncodeString(Char *s, UInt32 i);
		int EncodeString(Char *s, Float32 f);
		int EncodeString(Char *s, Float64 f);

		void DecomposeDouble(Float64 f, Int64 &frac, Int32 &x);
		void DecomposeFloat(float f, Int32 &frac, Int32 &x);

		template<class _Tinternal, class _Tstored>
		struct NumericTypeSerializer : public ITypeSerializer
		{
			void DeserializeTextInstance(OperationContext *ctx, ObjectManagement::IObjectManager *objm,
				void *instance, IO::ITextDeserializer *td, ObjectManagement::IPackageHost *host, ObjectManagement::Package *pkg) const
			{
				RDX_TRY(ctx)
				{
					_Tinternal v;
					ObjectManagement::CRef<const String> str;
					bool isString;
					RDX_PROTECT_ASSIGN(ctx, str, td->ParseToken(ctx, objm, &isString, true));
					DecodeString(static_cast<const Char *>(str->AsChars()), v);
					*static_cast<_Tinternal*>(instance) = v;
				}
				RDX_CATCH(ctx)
				{
					RDX_RETHROW(ctx);
				}
				RDX_ENDTRY
			}

			void DeserializeBinaryInstance(OperationContext *ctx, ObjectManagement::IObjectManager *objm, void *instance, IO::IFileStream *reader, ObjectManagement::IPackageHost *host, ObjectManagement::Package *pkg) const
			{
				bool overflowed = false;
				bool readFailed = false;
				if(!reader->ReadConverted<_Tstored, _Tinternal>(static_cast<_Tinternal*>(instance), overflowed, readFailed))
				{
					if(overflowed)
						RDX_LTHROW(ctx, Errors::RDX_ERROR_INTEGER_OVERFLOW);
					if(readFailed)
						RDX_LTHROW(ctx, Errors::RDX_ERROR_PACKAGE_CORRUPTED);
				}
			}

			virtual void SerializeBinaryInstance(ObjectManagement::IObjectManager *objm, const void *obj, IO::IFileStream *fs, const ObjectManagement::SSIDTable *ssidTable) const
			{
				fs->WriteConverted<_Tinternal, _Tstored>(*static_cast<const _Tinternal*>(obj));
			}

			virtual void SerializeTextInstance(ObjectManagement::IObjectManager *objm, const void *obj, IO::IFileStream *fs, const ObjectManagement::SSIDTable *ssidTable) const
			{
				Char chars[RDX_MAX_ENCODED_NUMBER_SIZE];
				UInt8 encoded[RDX_MAX_ENCODED_NUMBER_SIZE];

				EncodeString(chars, *static_cast<const _Tinternal*>(obj));
				LargeInt len;
				for(len=0;chars[len];len++)
					encoded[len] = static_cast<UInt8>(chars[len]);
				fs->WriteBytes(" ", 1);
				fs->WriteBytes(encoded, len);
			}
		};
	}
}
